package com.tuocent.dagv2.controller;

import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.entity.LoginUser;
import com.tuocent.dagv2.common.entity.LoginUserInfo;
import com.tuocent.dagv2.common.entity.ValidateCode;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.service.ILoginLogsService;
import com.tuocent.dagv2.service.IUsersService;
import com.tuocent.dagv2.service.IValidateCodeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("account")
public class AccountController {

    @Resource
    private RedisCache redis;
    @Autowired
    private IValidateCodeService validateCodeService;

    @Autowired
    private IUsersService usersService;
    @Autowired
    private ILoginLogsService loginLogsService;


    @GetMapping("/login")
    public String Login(Model model) {
        model.addAttribute("appName", PublicConst.APP_NAME);
        model.addAttribute("techSupport", PublicConst.TECH_SUPPORT);
        model.addAttribute("mail", PublicConst.EMAIL);
        model.addAttribute("serviceOnline", PublicConst.SERVICE_ONLINE);
        return "/account/login";
    }

    /**
     * 获取图片验证码
     *
     * @return AjaxResult
     */

    @GetMapping("/validateCode")
    @ResponseBody
    public AjaxResult get() {
        ValidateCode validateCode = validateCodeService.get();
        return AjaxResult.success(validateCode);
    }

    /**
     * 用户登录
     *
     * @param request
     * @param session
     * @param userInfo
     * @return
     */
    @PostMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(HttpServletRequest request, HttpSession session,
                                @RequestBody LoginUser userInfo) throws Exception {
        return usersService.userLogin(request, userInfo, session);
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request, HttpSession session, Model model) {
        LoginUserInfo loginUserInfo = redis.get(session.getId());
        if (loginUserInfo != null) {
            loginLogsService.loginLog(request, loginUserInfo.getUserCode(), "用户退出", "成功");
            redis.deleteObject(session.getId());
        }
        session.invalidate();
        model.addAttribute("appName", PublicConst.APP_NAME);
        model.addAttribute("techSupport", PublicConst.TECH_SUPPORT);
        model.addAttribute("mail", PublicConst.EMAIL);
        model.addAttribute("serviceOnline", PublicConst.SERVICE_ONLINE);
        return "/account/login";
    }

    @RequestMapping("passwordModify")
    public String PasswordModify() {
        return "/account/passwordModify";
    }

    @RequestMapping("setNewPassword")
    @ResponseBody
    public AjaxResult setNewPassword(HttpSession session,
                                     @RequestParam(value = "oldPwd") String oldPwd,
                                     @RequestParam(value = "newPwd") String newPwd) {
        return usersService.setNewPassword(session, oldPwd, newPwd);
    }



    /*
    @RequestMapping("checkUserPwd")
    @ResponseBody
    public AjaxResult checkUserPwd(HttpServletRequest request) {
        return usersService.checkUserPwd(request);
    }

     */

}
